<!--

  常用
  $refs => 对象类型，默认是一个空对象

  不常用
  1. $children
      因为：当前组件内的布局是可变的，不能保证下标值一致
      一般用于: 遍历子组件，做统一的某事: 验证
  2. $parent
      因为: 如果使用了这个，当前组件 就不够独立，被复用的几率就减少了
            当前组件通用性，就不强

      一般用于: 紧急/检查修复工具， 组件内常用 props/event,

-->

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <cpn ref='d'></cpn>
    <button @click='btnClick'>按钮</button>
  </div>

  <template id='cpn'>
    <div>
      我是子组件
    </div>
  </template>

  <script src="../../js/vue.js"></script>
  <script>

    const cpn = {
      template: '#cpn',
      methods: {
        showMessage() {
          console.log('---------------------------')
          console.log('showMessage----')
          console.log('parent=', this.$parent)
          let p = this.$parent || {};
          let print = p.print;
          print && print('揍你');
        }
      }
    }

    const app = new Vue({
      el: '#app',
      data: {

      },
      components: {
        cpn,
      },
      methods: {
        btnClick() {
          console.log('this.$children', this.$children)
          let children = this.$children[0] || {};
          let showMessage = children.showMessage;
          showMessage && showMessage();

          let refCpn = this.$refs.d || {};
          let show2 = refCpn.showMessage;
          show2 && show2();
        },
        print(e) {
          console.log('print e=', e)
        }
      }
    })
  </script>
</body>
</html>